import os

import allure
from selenium.webdriver.common.by import By

from utils.log_util import logger

black_list = [
    (By.XPATH, "//*[@resource-id = 'android:id/content']/child::*/child::*[2]"),
    (By.XPATH,  "//*[@text='马上升级']"),
    (By.XPATH, "//*[@text = '取消']"),
    (By.XPATH, "//*[@text='确定']")

]


# 声明一个黑名单
def black_wrapper(fun):
    def run(*args, **kwargs):
        # 传入第一个参数 self
        basepage = args[0]
        try:
            logger.info(f"开始查找元素：{args[1], args[2]}")
            return fun(*args, **kwargs)
        except Exception as e:
            logger.info("没有找到元素，开始异常处理")
            # # 获取项目路径
            # root_path = os.path.dirname(os.path.abspath(__file__))
            # # 截图
            # image_path = basepage.screenshot(root_path)
            # # 添加到报告中
            # allure.attach.file(image_path, name="查找元素异常截图", attachment_type=allure.attachment_type.PNG)
            #
            # # 保存 page source
            # page_source_path = basepage.save_page_source(root_path)
            # allure.attach.file(page_source_path, name="查找元素异常XML代码", attachment_type=allure.attachment_type.TEXT)
            # 遍历黑名单列表，查找里面的元素进行操作
            for black in black_list:
                eles = basepage.driver.find_elements(*black)
                if len(eles) > 0:
                    # 点击弹窗中的按钮
                    eles[0].click()
                    # 继续查找元素
                    return fun(*args, **kwargs)
            logger.info(f"遍历黑名单，仍然未找到元素，异常信息为{e}")
            raise e
    return run


